package org.light.generator;

import java.util.ArrayList;
import java.util.List;

import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.Statement;
import org.light.domain.StatementList;
import org.light.exception.ValidateException;
import org.light.utils.WriteableUtil;

public class ImageUtilGenerator extends Generator{	
	private static final long serialVersionUID = -4790994605747142428L;
	protected Domain sampleDomain;
	
	public Domain getSampleDomain() {
		return sampleDomain;
	}

	public void setSampleDomain(Domain sampleDomain) {
		this.sampleDomain = sampleDomain;
	}

	public ImageUtilGenerator(){
		super();		
		this.standardName = "ImageUtilGenerator";
		this.fileName = "ImageUtil.java";
	}	
	
	@Override
	public StatementList generateStatementList() throws ValidateException {
    	List<Writeable> sList = new ArrayList<Writeable>();
        sList.add(new Statement(1000L,0,"package "+this.sampleDomain.getPackageToken()+".utils;"));
        sList.add(new Statement(2000L,0,""));
        sList.add(new Statement(3000L,0,"import java.awt.Graphics;"));
        sList.add(new Statement(4000L,0,"import java.awt.image.BufferedImage;"));
        sList.add(new Statement(5000L,0,"import java.io.File;"));
        sList.add(new Statement(6000L,0,"import java.io.FileInputStream;"));
        sList.add(new Statement(7000L,0,"import java.io.FileOutputStream;"));
        sList.add(new Statement(8000L,0,"import java.io.IOException;"));
        sList.add(new Statement(9000L,0,"import java.io.InputStream;"));
        sList.add(new Statement(10000L,0,"import java.io.OutputStream;"));
        sList.add(new Statement(11000L,0,"import java.util.Base64;"));
        sList.add(new Statement(12000L,0,""));
        sList.add(new Statement(13000L,0,"import javax.imageio.ImageIO;"));
        sList.add(new Statement(14000L,0,""));
        sList.add(new Statement(15000L,0,"public class ImageUtil {"));
        sList.add(new Statement(16000L,1,"final static Base64.Decoder decoder = Base64.getDecoder();"));
        sList.add(new Statement(17000L,1,"final static Base64.Encoder encoder = Base64.getEncoder();"));
        sList.add(new Statement(18000L,1,"/**"));
        sList.add(new Statement(19000L,1,"* 将图片文件转为字符串"));
        sList.add(new Statement(20000L,1,"* @param imgFile"));
        sList.add(new Statement(21000L,1,"* @return"));
        sList.add(new Statement(22000L,1,"*/"));
        sList.add(new Statement(23000L,1,"public static String getImageStr(String imgFile) {"));
        sList.add(new Statement(24000L,2,"// 将图片文件转化为字节数组字符串，并对其进行Base64编码处理"));
        sList.add(new Statement(25000L,2,"//String imgFile = \"d:\\111.jpg\";// 待处理的图片"));
        sList.add(new Statement(26000L,2,"InputStream in = null;"));
        sList.add(new Statement(27000L,2,"byte[] data = null;"));
        sList.add(new Statement(28000L,2,"// 读取图片字节数组"));
        sList.add(new Statement(29000L,2,"try {"));
        sList.add(new Statement(30000L,3,"in = new FileInputStream(imgFile);"));
        sList.add(new Statement(31000L,3,"data = new byte[in.available()];"));
        sList.add(new Statement(32000L,3,"in.read(data);"));
        sList.add(new Statement(33000L,3,"in.close();"));
        sList.add(new Statement(34000L,2,"} catch (IOException e) {"));
        sList.add(new Statement(35000L,3,"e.printStackTrace();"));
        sList.add(new Statement(36000L,2,"}"));
        sList.add(new Statement(37000L,2,"// 返回Base64编码过的字节数组字符串"));
        sList.add(new Statement(38000L,2,"return encoder.encode(data).toString();"));
        sList.add(new Statement(39000L,1,"}"));
        sList.add(new Statement(40000L,1,""));
        sList.add(new Statement(41000L,1,"/**"));
        sList.add(new Statement(42000L,1,"* 将图片文件转为字符串"));
        sList.add(new Statement(43000L,1,"* @param imgFile"));
        sList.add(new Statement(44000L,1,"* @return"));
        sList.add(new Statement(45000L,1,"*/"));
        sList.add(new Statement(46000L,1,"public static String getImageStr(byte [] imgBytes) {"));
        sList.add(new Statement(47000L,2,"return encoder.encodeToString(imgBytes);"));
        sList.add(new Statement(48000L,1,"}"));
        sList.add(new Statement(49000L,1,""));
        sList.add(new Statement(50000L,1,"/**"));
        sList.add(new Statement(51000L,1,"* 将图片文件转为byte数字"));
        sList.add(new Statement(52000L,1,"* @param imgFile"));
        sList.add(new Statement(53000L,1,"* @return"));
        sList.add(new Statement(54000L,1,"*/"));
        sList.add(new Statement(55000L,1,"public static byte[] getImageByte(String imgFile) {"));
        sList.add(new Statement(56000L,2,"// 将图片文件转化为字节数组字符串，并对其进行Base64编码处理"));
        sList.add(new Statement(57000L,2,"//String imgFile = \"d:\\111.jpg\";// 待处理的图片"));
        sList.add(new Statement(58000L,2,"InputStream in = null;"));
        sList.add(new Statement(59000L,2,"byte[] data = null;"));
        sList.add(new Statement(60000L,2,"// 读取图片字节数组"));
        sList.add(new Statement(61000L,2,"try {"));
        sList.add(new Statement(62000L,3,"in = new FileInputStream(imgFile);"));
        sList.add(new Statement(63000L,3,"data = new byte[in.available()];"));
        sList.add(new Statement(64000L,3,"in.read(data);"));
        sList.add(new Statement(65000L,3,"in.close();"));
        sList.add(new Statement(66000L,2,"} catch (IOException e) {"));
        sList.add(new Statement(67000L,3,"e.printStackTrace();"));
        sList.add(new Statement(68000L,2,"}"));
        sList.add(new Statement(69000L,2,"// 返回Base64编码过的字节数组字符串"));
        sList.add(new Statement(70000L,2,"return data;"));
        sList.add(new Statement(71000L,1,"}"));
        sList.add(new Statement(72000L,1,""));
        sList.add(new Statement(73000L,1,"/**"));
        sList.add(new Statement(74000L,1,"* 将字符串转为图片"));
        sList.add(new Statement(75000L,1,"* @param imgStr"));
        sList.add(new Statement(76000L,1,"* @return"));
        sList.add(new Statement(77000L,1,"*/"));
        sList.add(new Statement(78000L,1,"public static boolean generateImage(String imgStr,String imgFile)throws Exception {"));
        sList.add(new Statement(79000L,2,"// 对字节数组字符串进行Base64解码并生成图片"));
        sList.add(new Statement(80000L,2,"if (imgStr == null) // 图像数据为空"));
        sList.add(new Statement(81000L,3,"return false;"));
        sList.add(new Statement(82000L,2,"try {"));
        sList.add(new Statement(83000L,3,"// Base64解码"));
        sList.add(new Statement(84000L,3,"byte[] b = decoder.decode(imgStr);"));
        sList.add(new Statement(85000L,3,"for (int i = 0; i < b.length; ++i) {"));
        sList.add(new Statement(86000L,4,"if (b[i] < 0) {// 调整异常数据"));
        sList.add(new Statement(87000L,5,"b[i] += 256;"));
        sList.add(new Statement(88000L,4,"}"));
        sList.add(new Statement(89000L,3,"}"));
        sList.add(new Statement(90000L,3,"// 生成jpeg图片"));
        sList.add(new Statement(91000L,3,"String imgFilePath = imgFile;// 新生成的图片"));
        sList.add(new Statement(92000L,3,"OutputStream out = new FileOutputStream(imgFilePath);"));
        sList.add(new Statement(93000L,3,"out.write(b);"));
        sList.add(new Statement(94000L,3,"out.flush();"));
        sList.add(new Statement(95000L,3,"out.close();"));
        sList.add(new Statement(96000L,3,"return true;"));
        sList.add(new Statement(97000L,2,"} catch (Exception e) {"));
        sList.add(new Statement(98000L,3,"throw e;"));
        sList.add(new Statement(99000L,2,"}"));
        sList.add(new Statement(100000L,1,"}"));
        sList.add(new Statement(101000L,1,""));
        sList.add(new Statement(102000L,1,"/**"));
        sList.add(new Statement(103000L,1,"* 图片是否符合 jpg gjf png格式"));
        sList.add(new Statement(104000L,1,"* @param imgStr"));
        sList.add(new Statement(105000L,1,"* @return"));
        sList.add(new Statement(106000L,1,"*/"));
        sList.add(new Statement(107000L,1,"public static boolean isRightFormat(String format){"));
        sList.add(new Statement(108000L,1,""));
        sList.add(new Statement(109000L,2,"return (format.equals(\"jpg\") || format.equals(\"gif\") || format.equals(\"png\"))?true:false;"));
        sList.add(new Statement(110000L,1,"}"));
        sList.add(new Statement(111000L,1,""));
        sList.add(new Statement(112000L,1,"/**"));
        sList.add(new Statement(113000L,1,"* 对图片进行放大"));
        sList.add(new Statement(114000L,1,"* @param originalImage 原始图片"));
        sList.add(new Statement(115000L,1,"* @param times 放大倍数"));
        sList.add(new Statement(116000L,1,"* @return"));
        sList.add(new Statement(117000L,1,"*/"));
        sList.add(new Statement(118000L,1,""));
        sList.add(new Statement(119000L,1,"public static BufferedImage  zoomInImage(BufferedImage  originalImage, Integer times){"));
        sList.add(new Statement(120000L,2,""));
        sList.add(new Statement(121000L,2,"int width = originalImage.getWidth()*times;"));
        sList.add(new Statement(122000L,2,"int height = originalImage.getHeight()*times;"));
        sList.add(new Statement(123000L,2,""));
        sList.add(new Statement(124000L,2,"BufferedImage newImage = new BufferedImage(width,height,originalImage.getType());"));
        sList.add(new Statement(125000L,2,"Graphics g = newImage.getGraphics();"));
        sList.add(new Statement(126000L,2,"g.drawImage(originalImage, 0,0,width,height,null);"));
        sList.add(new Statement(127000L,2,"g.dispose();"));
        sList.add(new Statement(128000L,2,"return newImage;"));
        sList.add(new Statement(129000L,1,"}"));
        sList.add(new Statement(130000L,1,""));
        sList.add(new Statement(131000L,1,"/**"));
        sList.add(new Statement(132000L,1,"* 对图片进行放大"));
        sList.add(new Statement(133000L,1,"* @param srcPath 原始图片路径(绝对路径)"));
        sList.add(new Statement(134000L,1,"* @param newPath 放大后图片路径（绝对路径）"));
        sList.add(new Statement(135000L,1,"* @param times 放大倍数"));
        sList.add(new Statement(136000L,1,"* @return 是否放大成功"));
        sList.add(new Statement(137000L,1,"*/"));
        sList.add(new Statement(138000L,1,""));
        sList.add(new Statement(139000L,1,"public static boolean zoomInImage(String srcPath,String newPath,Integer times){"));
        sList.add(new Statement(140000L,2,""));
        sList.add(new Statement(141000L,2,"BufferedImage bufferedImage = null;"));
        sList.add(new Statement(142000L,2,"try{"));
        sList.add(new Statement(143000L,3,""));
        sList.add(new Statement(144000L,3,"File of = new File(srcPath);"));
        sList.add(new Statement(145000L,3,"if(of.canRead()){"));
        sList.add(new Statement(146000L,4,""));
        sList.add(new Statement(147000L,4,"bufferedImage =  ImageIO.read(of);"));
        sList.add(new Statement(148000L,4,""));
        sList.add(new Statement(149000L,3,"}"));
        sList.add(new Statement(150000L,2,"}catch(Exception e){"));
        sList.add(new Statement(151000L,3,"//TODO: 打印日志"));
        sList.add(new Statement(152000L,3,"return false;"));
        sList.add(new Statement(153000L,2,"}"));
        sList.add(new Statement(154000L,2,"if(bufferedImage != null){"));
        sList.add(new Statement(155000L,3,""));
        sList.add(new Statement(156000L,3,"bufferedImage = zoomInImage(bufferedImage,times);"));
        sList.add(new Statement(157000L,3,"try {"));
        sList.add(new Statement(158000L,5,"//TODO: 这个保存路径需要配置下子好一点"));
        sList.add(new Statement(159000L,5,"//保存修改后的图像,全部保存为JPG格式"));
        sList.add(new Statement(160000L,5,"ImageIO.write(bufferedImage, \"JPG\", new File(newPath));"));
        sList.add(new Statement(161000L,4,"} catch (IOException e) {"));
        sList.add(new Statement(162000L,5,"// TODO 打印错误信息"));
        sList.add(new Statement(163000L,5,"return false;"));
        sList.add(new Statement(164000L,4,"}"));
        sList.add(new Statement(165000L,2,"}"));
        sList.add(new Statement(166000L,2,"return true;"));
        sList.add(new Statement(167000L,2,""));
        sList.add(new Statement(168000L,1,"}"));
        sList.add(new Statement(169000L,1,""));
        sList.add(new Statement(170000L,1,"/**"));
        sList.add(new Statement(171000L,1,"* 对图片进行缩小"));
        sList.add(new Statement(172000L,1,"* @param originalImage 原始图片"));
        sList.add(new Statement(173000L,1,"* @param times 缩小倍数"));
        sList.add(new Statement(174000L,1,"* @return 缩小后的Image"));
        sList.add(new Statement(175000L,1,"*/"));
        sList.add(new Statement(176000L,1,"public static BufferedImage  zoomOutImage(BufferedImage  originalImage, Integer times){"));
        sList.add(new Statement(177000L,2,"int width = originalImage.getWidth()/times;"));
        sList.add(new Statement(178000L,2,"int height = originalImage.getHeight()/times;"));
        sList.add(new Statement(179000L,2,"BufferedImage newImage = new BufferedImage(width,height,originalImage.getType());"));
        sList.add(new Statement(180000L,2,"Graphics g = newImage.getGraphics();"));
        sList.add(new Statement(181000L,2,"g.drawImage(originalImage, 0,0,width,height,null);"));
        sList.add(new Statement(182000L,2,"g.dispose();"));
        sList.add(new Statement(183000L,2,"return newImage;"));
        sList.add(new Statement(184000L,1,"}"));
        sList.add(new Statement(185000L,1,""));
        sList.add(new Statement(186000L,1,"/**"));
        sList.add(new Statement(187000L,1,"* 对图片进行放大"));
        sList.add(new Statement(188000L,1,"* @param srcPath 原始图片路径(绝对路径)"));
        sList.add(new Statement(189000L,1,"* @param newPath 放大后图片路径（绝对路径）"));
        sList.add(new Statement(190000L,1,"* @param times 放大倍数"));
        sList.add(new Statement(191000L,1,"* @return 是否放大成功"));
        sList.add(new Statement(192000L,1,"*/"));
        sList.add(new Statement(193000L,1,""));
        sList.add(new Statement(194000L,1,"public static boolean zoomOutImage(String srcPath,String newPath,Integer times){"));
        sList.add(new Statement(195000L,2,"BufferedImage bufferedImage = null;"));
        sList.add(new Statement(196000L,2,"try {"));
        sList.add(new Statement(197000L,3,"File of = new File(srcPath);"));
        sList.add(new Statement(198000L,3,"if(of.canRead()){"));
        sList.add(new Statement(199000L,4,"bufferedImage =  ImageIO.read(of);"));
        sList.add(new Statement(200000L,3,"}"));
        sList.add(new Statement(201000L,2,"} catch (IOException e) {"));
        sList.add(new Statement(202000L,3,"//TODO: 打印日志"));
        sList.add(new Statement(203000L,3,"return false;"));
        sList.add(new Statement(204000L,2,"}"));
        sList.add(new Statement(205000L,2,"if(bufferedImage != null){"));
        sList.add(new Statement(206000L,3,"bufferedImage = zoomOutImage(bufferedImage,times);"));
        sList.add(new Statement(207000L,3,"try {"));
        sList.add(new Statement(208000L,4,"//TODO: 这个保存路径需要配置下子好一点"));
        sList.add(new Statement(209000L,4,"//保存修改后的图像,全部保存为JPG格式"));
        sList.add(new Statement(210000L,4,"ImageIO.write(bufferedImage, \"JPG\", new File(newPath));"));
        sList.add(new Statement(211000L,3,"} catch (IOException e) {"));
        sList.add(new Statement(212000L,4,"// TODO 打印错误信息"));
        sList.add(new Statement(213000L,4,"return false;"));
        sList.add(new Statement(214000L,3,"}"));
        sList.add(new Statement(215000L,2,"}"));
        sList.add(new Statement(216000L,2,"return true;"));
        sList.add(new Statement(217000L,1,"}"));
        sList.add(new Statement(230000L,0,"}"));
              
    	return WriteableUtil.merge(sList);
	}
}
